package cn.ronghuanet.org.controller;

import cn.ronghuanet.org.domain.Department;
import cn.ronghuanet.org.query.DepartmentQuery;
import cn.ronghuanet.org.service.IDepartmentService;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //当前类默认返回json格式
@RequestMapping("/department")
@Api(tags = "部门管理-蓉易学")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //1.查询所有
    //localhost:8080/department   get请求方式
    @GetMapping //get请求
    @ApiOperation(value = "查询所有")
    public AjaxResult loadAll(){
        //返回值,默认就是成功
        AjaxResult ajaxResult=AjaxResult.me();
        try {
            List<Department> list = departmentService.loadAll();
            ajaxResult.setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败 {"success":false,"message":.....}
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("网络异常,请稍后重试!");
        }
        return ajaxResult;
    }
    //2.查询单个
    //localhost:8080/department/1   get请求方式
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public AjaxResult getOne(@PathVariable("id") Long id){
        //返回值,默认就是成功
        AjaxResult ajaxResult=AjaxResult.me();
        try {
            Department one = departmentService.getOne(id);
            ajaxResult.setResultObj(one);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败 {"success":false,"message":.....}
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("网络异常,请稍后重试!");
        }
        return ajaxResult;
    }
    //3.新增or修改
    //localhost:8080/department   put请求方式
    @PutMapping
    @ApiOperation(value = "新增or修改")
    public AjaxResult saveDepartment(@RequestBody Department department){
        //返回值,默认就是成功
        AjaxResult ajaxResult=AjaxResult.me();
        try {
            //新增没有id,修改有id
            Long id = department.getId();
            if (id==null){
                departmentService.insertOne(department);
            }else {
                departmentService.updateOne(department);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败 {"success":false,"message":.....}
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("网络异常,请稍后重试!");
        }
        return ajaxResult;
    }
    //4.删除单个
    //localhost:8080/department/1  delete请求方式
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个")
    public AjaxResult deleteDepartmentById(@PathVariable("id") Long id){
        //返回值,默认就是成功
        AjaxResult ajaxResult=AjaxResult.me();
        try {
            departmentService.deleteOne(id);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败 {"success":false,"message":.....}
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("网络异常,请稍后重试!");
        }
        return ajaxResult;
    }
    //5.批量删除
    //localhost:8080/department   Patch请求方式
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        //返回值,默认就是成功
        AjaxResult ajaxResult=AjaxResult.me();
        try {
            departmentService.patchDel(ids);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败 {"success":false,"message":.....}
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("网络异常,请稍后重试!");
        }
        return ajaxResult;
    }

    //6.分页查询
    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        //返回值,默认就是成功
        AjaxResult ajaxResult=AjaxResult.me();
        try {
            PageList<Department> list = departmentService.queryPage(departmentQuery);
            ajaxResult.setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败 {"success":false,"message":.....}
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("网络异常,请稍后重试!");
        }
        return ajaxResult;
    }

    //7.部门树查询
    @GetMapping("/deptTree/{id}")
    @ApiOperation(value = "部门树查询")
    public AjaxResult deptTree(@PathVariable("id") Long id){
        //返回值,默认就是成功
        AjaxResult ajaxResult=AjaxResult.me();
        try {
            List<Department> list = departmentService.deptTree(id);
            ajaxResult.setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败 {"success":false,"message":.....}
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("网络异常,请稍后重试!");
        }
        return ajaxResult;
    }
}
